package com.yylending.las.lending;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.statistics.StatService;
import com.yylending.las.Constants;
import com.yylending.las.R;
import com.yylending.las.StatIDSet;
import com.yylending.las.base.BaseFragment;
import com.yylending.las.common.widgets.CircleLoading;
import com.yylending.las.common.widgets.RedDotView;
import com.yylending.las.common.widgets.TelephoneTextView;
import com.yylending.las.lending.quota.myquota.MyQuotaModel;
import com.yylending.las.main.MainController;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 借钱页面-额度计算中
 * Created by laixj on 2017/5/22.
 * Copyright ©2017 www.yylending.com. All Rights Reserved.
 */

public class CalculatingFragment extends BaseFragment {

    static final String TAG = CalculatingFragment.class.getSimpleName();

    private final static long ANIMATION_DELAY = 1500L;
    static final int LOADER_ID_MY_QUOTA = 1;

    Unbinder unbinder;
    @BindView(R.id.title_left_iv)
    ImageView mTitleLeftIv;
    @BindView(R.id.title_tv)
    TextView mTitleTv;
    @BindView(R.id.title_right_tv)
    TextView mTitleRightTv;
    @BindView(R.id.title_right_iv)
    ImageView mTitleRightIv;
    @BindView(R.id.layout_title_bar)
    RelativeLayout mLayoutTitleBar;
    @BindView(R.id.calculate_hint_iv)
    ImageView mCalculateHintIv;
    @BindView(R.id.lending_notice_tv)
    TextView mLendingNoticeTv;
    @BindView(R.id.calculate_hint_tv)
    TextView mCalculateHintTv;
    @BindView(R.id.circle_loading)
    CircleLoading mCircleLoading;
    @BindView(R.id.calculate_anim_rl)
    RelativeLayout mCalculateAnimRl;
    @BindView(R.id.service_phone_tv)
    TelephoneTextView mServicePhoneTv;
    @BindView(R.id.red_dot_view)
    RedDotView mRedDotView;

    /**
     * 额度状态
     */
    private MyQuotaModel.ApplyStatus mApplyStatus;
    /**
     * 预计额度计算时间
     */
    private String mCalculateTime = "1分钟";

    public static BaseFragment newInstance() {
        BaseFragment fragment = new CalculatingFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

    }

    @Override
    public void onDetach() {

        super.onDetach();
    }

    @Override
    public void onStart() {
        super.onStart();
        initAnim();
    }

    @Override
    public void onStop() {
        clearAnim();
        super.onStop();
    }

    private void initAnim() {
        // 实例化RotateAnimation对象，参数为（旋转的起始角度，旋转的角度，旋转中心的类型（这里为根据自己），0.5f表示x方向或者y方向的中心）
        RotateAnimation animation = new RotateAnimation(0, 360,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, (float) 0.5);
        //设置动画的执行时间
        animation.setDuration(ANIMATION_DELAY);
        animation.setInterpolator(new LinearInterpolator());//不停顿
        animation.setRepeatCount(-1);//重复次数
        //animation.setFillAfter(true);//停在最后
        //把动画加载到控件上，并开启动画
        mCircleLoading.startAnimation(animation);
    }

    private void clearAnim() {
        mCircleLoading.clearAnimation();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.view_stub_limit_calculating, container, false);
        unbinder = ButterKnife.bind(this, rootView);
        initView();
        return rootView;
    }

    private void initView() {
        mTitleLeftIv.setImageResource(R.drawable.ic_home_help);
        mTitleTv.setText(R.string.tab_lend);
        mTitleRightIv.setImageResource(R.drawable.ic_message_white);
        mServicePhoneTv.setPageNo(Constants.PAGE_NO_MAIN_ACTIVITY);
        mCalculateHintTv.setText(getString(R.string.quota_calculate_time, mCalculateTime));
        MainController.initRedDot(mRedDotView);
    }

    @Override
    public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {
        switch (loader.getId()) {
            case LOADER_ID_MY_QUOTA:
                break;
        }
    }

    @Override
    public Loader<Presenter> onCreateLoader(int id, Bundle args) {
        switch (id) {
            case LOADER_ID_MY_QUOTA:
            default:
                return null;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.title_left_iv, R.id.title_right_iv, R.id.calculate_hint_iv, R.id.lending_notice_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_iv:
                EventAgent.post(new QuotaEvent.PageJumpEvent(QuotaEvent.PageJumpEvent.QUOTA_JUMP_HELP_PAGE, Constants.PAGE_NO_MAIN_ACTIVITY));
                break;
            case R.id.title_right_iv:
                EventAgent.post(new QuotaEvent.PageJumpEvent(QuotaEvent.PageJumpEvent.QUOTA_JUMP_MESSAGE_PAGE, Constants.PAGE_NO_MAIN_ACTIVITY));
                MainController.hideRedDot(mRedDotView);
                break;
            case R.id.calculate_hint_iv:
                StatService.onEvent(getContext(), StatIDSet.EVENT_ID_TO_CHECK_CALCULATION_AMOUNT);
                showOneBtnDialog(getString(R.string.calculate_time_notice), getString(R.string.got_it));
                break;
            case R.id.lending_notice_tv:
                EventAgent.post(new QuotaEvent.ShowLoanNoticeEvent(QuotaEvent.ShowLoanNoticeEvent.QUOTA_SHOW_LOAN_NOTICE, Constants.PAGE_NO_MAIN_ACTIVITY));
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onRedDotShowEvent(QuotaEvent.ShowRedDotEvent event){
        mRedDotView.setVisibility(View.VISIBLE);
    }
}